package cn.javadog.algorithm.m1_100;


/**
 * 整数转罗马数字
 * https://leetcode-cn.com/problems/integer-to-roman/
 * @author Sprite
 */
public class Algorithm12 {
	public static void main(String[] args) {
		System.out.println(intToRoman(3));
		System.out.println(intToRoman(4));
		System.out.println(intToRoman(9));
		System.out.println(intToRoman(58));
		System.out.println(intToRoman(1994));
	}

	/**
	 * 贪心算法太漂亮
	 */
	public static String intToRoman(int num) {
		int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
		String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 13; i++) {
			int b = num / nums[i];
			for (int j = 0; j < b; j++, sb.append(romans[i]));
			num = num % nums[i];
		}

		return sb.toString();
	}
}
